﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManager
{
    public partial class AddEditProvider : Form
    {
        private List<PictureBox> _listpbxStar;
        List<string[]> _categoryTree = new List<string[]>();

        public AddEditProvider()
        {
            InitializeComponent();
            _categoryTree.Add(new string[] { "/", "/" });
            _listpbxStar = new List<PictureBox>() { pbxStar1, pbxStar2, pbxStar3, pbxStar4, pbxStar5 };
            ShowStar(0);
        }

        public void RefreshProviderCategory()
        {
            DataTable categories = DBAccess.GetProviderCategory(txtID.Text);
            if (categories != null)
            {
                lvProviderCategory.Items.Clear();
                foreach (DataRow row in categories.Rows)
                {
                    lvProviderCategory.Items.Add(new ListViewItem(new string[] {
                        row["Name"].ToString(), row["ID"].ToString() }));
                }
            }
        }

        private DataTable GetChildCategory(string parentID)
        {
            if (parentID == "/")
            {
                return DBAccess.GetRootCategory();
            }
            else
            {
                return DBAccess.GetChildCategory(parentID);
            }
        }

        private void RefreshAllCategory()
        {
            DataTable child;
            string indent = "";

            lvAllCategory.Items.Clear();
            foreach (string[] category in _categoryTree)
            {
                lvAllCategory.Items.Add(new ListViewItem(category));
            }
            if (_categoryTree.Count > 1)
            {
                indent = "|--";
                for (int i = 0; i < _categoryTree.Count - 2; i++)
                {
                    indent = "    " + indent;
                }
            }
            child = GetChildCategory(_categoryTree[_categoryTree.Count - 1][1]);
            foreach (DataRow row in child.Rows)
            {
                lvAllCategory.Items.Add(new ListViewItem(new string[] {
                    indent + row["Name"].ToString(), row["ID"].ToString() }));
            }
        }

        public void InitAdd()
        {
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnAddCategory.Enabled = false;
            btnDeleteCategory.Enabled = false;
            RefreshAllCategory();
        }

        public bool InitEdit(string id)
        {
            btnAdd.Enabled = false;
            txtID.Text = id;
            DataTable provider = DBAccess.GetRowWithID("Provider", id);
            if (provider != null && provider.Rows.Count > 0)
            {
                txtName.Text = provider.Rows[0]["Name"].ToString();
                txtAddress.Text = provider.Rows[0]["Address"].ToString();
                txtWebsite.Text = provider.Rows[0]["Website"].ToString();
                txtRating.Text = provider.Rows[0]["Rating"].ToString();
                RefreshAllCategory();
                RefreshProviderCategory();
            }
            else
            {
                return false;
            }
            return true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (DBAccess.AddProvider(txtName.Text, txtAddress.Text, txtWebsite.Text, txtRating.Text))
            {
                MessageBox.Show("Add provider successful!!!");
                txtID.Text = DBAccess.GetLastRow("Provider").Rows[0]["ID"].ToString();
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
                btnAddCategory.Enabled = true;
                btnDeleteCategory.Enabled = true;
                RefreshAllCategory();
            }
            else
            {
                MessageBox.Show("Add provider failed!!!");
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (DBAccess.UpdateProvider(txtID.Text, txtName.Text, txtAddress.Text, txtWebsite.Text, txtRating.Text))
            {
                MessageBox.Show("Update provider successful!!!");
            }
            else
            {
                MessageBox.Show("Update provider failed!!!");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DBAccess.DeleteProvider(txtID.Text))
            {
                MessageBox.Show("Delete provider successful!!!");
            }
            else
            {
                MessageBox.Show("Delete provider failed!!!");
            }
            Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ShowStar(int level)
        {
            for (int i = 0; i < _listpbxStar.Count; i++)
                _listpbxStar[i].Visible = false;
            for (int i = 0; i < level; i++)
                _listpbxStar[i].Visible = true;
        }

        private void txtRating_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtRating.Text = "";
            if (e.KeyChar != '1' && e.KeyChar != '2' && e.KeyChar != '3' && e.KeyChar != '4' && e.KeyChar != '5')
                e.Handled = true;
        }

        private void txtRating_TextChanged(object sender, EventArgs e)
        {
            if (txtRating.Text != "")
                ShowStar(int.Parse(txtRating.Text));
            else
                ShowStar(0);
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            if (lvAllCategory.SelectedIndices.Count == 1)
            {
                DBAccess.AddProviderCategory(txtID.Text, lvAllCategory.SelectedItems[0].SubItems[1].Text);
                RefreshProviderCategory();
            }
        }

        private void btnDeleteCategory_Click(object sender, EventArgs e)
        {
            if (lvProviderCategory.SelectedItems.Count == 1)
            {
                if (DBAccess.DeleteProviderCategory(txtID.Text, lvProviderCategory.SelectedItems[0].SubItems[1].Text))
                {
                    RefreshProviderCategory();
                }
                else
                {
                    MessageBox.Show("Delete category failed!!!");
                }
            }
            else
            {
                MessageBox.Show("Please choose one category!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lvAllCategory_DoubleClick(object sender, EventArgs e)
        {
            if (lvAllCategory.SelectedIndices.Count == 1)
            {
                if (lvAllCategory.SelectedIndices[0] < _categoryTree.Count)
                {
                    while (_categoryTree.Count - 1 > lvAllCategory.SelectedIndices[0])
                    {
                        _categoryTree.RemoveAt(_categoryTree.Count - 1);
                    }
                }
                else
                {
                    _categoryTree.Add(new string[] {
                        lvAllCategory.SelectedItems[0].SubItems[0].Text,
                        lvAllCategory.SelectedItems[0].SubItems[1].Text });
                }
                RefreshAllCategory();
            }
        }
    }
}
